package com.testplatform.aitestplatform.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.testplatform.aitestplatform.domain.TestRequset;
import com.testplatform.aitestplatform.utils.BaseTest;
import io.restassured.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.testplatform.aitestplatform.mapper.ApiTestCaseMapper;
import com.testplatform.aitestplatform.domain.ApiTestCase;
import com.testplatform.aitestplatform.service.IApiTestCaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 接口自动化测试用例Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-26
 */
@Service
public class ApiTestCaseServiceImpl extends ServiceImpl<ApiTestCaseMapper, ApiTestCase> implements IApiTestCaseService
{
    @Autowired
    private ApiTestCaseMapper apiTestCaseMapper;

    /**
     * 查询接口自动化测试用例
     * 
     * @param id 接口自动化测试用例主键
     * @return 接口自动化测试用例
     */
    @Override
    public ApiTestCase selectApiTestCaseById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询接口自动化测试用例列表
     * 
     * @param apiTestCase 接口自动化测试用例
     * @return 接口自动化测试用例
     */
    @Override
    public List<ApiTestCase> selectApiTestCaseList(ApiTestCase apiTestCase) {
//        LambdaQueryWrapper<ApiTestCase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper
//                .like(ApiTestCase::getSerialNumber, apiTestCase.getSerialNumber())
//                .like(ApiTestCase::getProject, apiTestCase.getProject())
//                .like(ApiTestCase::getModule, apiTestCase.getModule())
//                .like(ApiTestCase::getTitle, apiTestCase.getTitle())
//                .eq(ApiTestCase::getAutoRunEnabled, apiTestCase.getAutoRunEnabled())
//                .eq(ApiTestCase::getIsActive, apiTestCase.getIsActive());
//        lambdaQueryWrapper.orderByDesc(ApiTestCase::getUpdateTime);
//        return apiTestCaseMapper.selectList(lambdaQueryWrapper);
        return apiTestCaseMapper.selectApiTestCaseList(apiTestCase);
    }

    /**
     * 新增接口自动化测试用例
     * 
     * @param apiTestCase 接口自动化测试用例
     * @return 结果
     */
    @Override
    public int insertApiTestCase(ApiTestCase apiTestCase)
    {
        return save(apiTestCase) ? 1 : 0;
    }

    /**
     * 修改接口自动化测试用例
     * 
     * @param apiTestCase 接口自动化测试用例
     * @return 结果
     */
    @Override
    public int updateApiTestCase(ApiTestCase apiTestCase)
    {
        return updateById(apiTestCase) ? 1 : 0;
    }

    /**
     * 批量删除接口自动化测试用例
     * 
     * @param ids 需要删除的接口自动化测试用例主键
     * @return 结果
     */
    @Override
    public int deleteApiTestCaseByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除接口自动化测试用例信息
     * 
     * @param id 接口自动化测试用例主键
     * @return 结果
     */
    @Override
    public int deleteApiTestCaseById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public int executionApiTestCase(ApiTestCase apiTestCase) {
        String apiUrl = apiTestCase.getApiUrl();
        String requestHeader = apiTestCase.getRequestHeader();
        String requestMethod = apiTestCase.getRequestMethod();
        String requestBody = apiTestCase.getRequestBody();
        TestRequset testRequset = new TestRequset(apiUrl, requestMethod, requestBody, JSONObject.parseObject(requestHeader, Map.class), null, null);
        Response response = BaseTest.request(testRequset);
        int code = response.statusCode();
        if (code != 200){
            apiTestCase.setIsActive(0);
            apiTestCaseMapper.updateById(apiTestCase);
            return 0;
        }

        if (code == 200){
            apiTestCase.setIsActive(1);
            apiTestCaseMapper.updateById(apiTestCase);
        }
        return 1;

    }
}
